package com.fantastic.service.lwf.impl;

import com.fantastic.common.utils.FilesUtils;
import com.fantastic.mapper.lwf.SetMealInfoMapper;
import com.fantastic.mapper.zjs.OrderDetailsMapper;
import com.fantastic.mapper.zjs.OrdersMapper;
import com.fantastic.mapper.zjs.VarietyMapper;
import com.fantastic.model.pojo.lwf.SetMeal;
import com.fantastic.model.pojo.zjs.OrderDetails;
import com.fantastic.model.pojo.zjs.Orders;
import com.fantastic.model.pojo.zjs.Variety;
import com.fantastic.model.vo.lwf.app.orderdetails.OrderDetailsVo;
import com.fantastic.service.lwf.OrderDetailsInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author .FengYe.
 * @date 2024-06-21
 */
@Service
public class OrderDetailsInfoServiceImpl implements OrderDetailsInfoService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private OrderDetailsMapper orderDetailsMapper;

    @Resource
    private VarietyMapper varietyMapper;

    @Resource
    private SetMealInfoMapper setMealInfoMapper;

    @Override
    public OrderDetailsVo getOrderDetailsByoId(String oId) {
        System.out.println("================");
        System.out.println(oId);
        System.out.println("================");
        // 查询订单信息表
        Orders orders = ordersMapper.getOrders(oId);
        // 根据订单 id 查找订单详情
        OrderDetails order = new OrderDetails();
        order.setOrderId(oId);
        List<OrderDetails> orderDetails = orderDetailsMapper.listOrderDetails(order);

        // 实例化 订单详情
        OrderDetailsVo orderDetailsVo = new OrderDetailsVo();
        orderDetailsVo.setTakeFoodNum(orders.getOrderId())
                .setOrderStatus(orders.getOrderStatus() == 0 ? "订单已完成，喜欢您再来！" : "订单正在火速制作中，请稍等...")
                .setOPrice(orders.getOrderPays())
                .setOWay(orders.getOrderWay() == 1 ? "堂食" : "外送")
                .setOTime(orders.getOrderTime());

        ArrayList<Variety> varietyArrayList = new ArrayList<>();
        ArrayList<SetMeal> setMealArrayList = new ArrayList<>();

        // 循环菜品信息
        for (OrderDetails orderDetail : orderDetails) {
            // 判断菜品 id 是否为空
            if (orderDetail.getVarietyId() != null) {
                // 获取菜品信息
                Variety variety = varietyMapper.getVariety(orderDetail.getVarietyId());
                variety.setVarietyPicture(FilesUtils.getSlashParse(variety.getVarietyPicture()));
                // 获取该订单详情的支付金额
                variety.setVarietyPrice(orderDetail.getOrderDetailsPrice());
                // 获取该订单的数量
                variety.setVarietySell(orderDetail.getOrderDetailsCount());

                // 添加到集合
                varietyArrayList.add(variety);
            } else {
                // 实例化 套餐点单详情
                ArrayList<String> takeFoodSetMeal = new ArrayList<>();

                // 获取套餐信息
                SetMeal setMeal = setMealInfoMapper.getSetMeal(orderDetail.getMealId());
                setMeal.setMealPic(FilesUtils.getSlashParse(setMeal.getMealPic()));
                // 所选套餐数量
                setMeal.setCount(orderDetail.getOrderDetailsCount());

                // 获取套餐选择详情 并拆分
                String orderDetailsRemark = orderDetail.getOrderDetailsRemark();
                String[] split = orderDetailsRemark.split("\\s+");

                for (String s : split) {
                    // 根据 * 拆分
                    String[] sp = s.split("\\*");
                    takeFoodSetMeal.add(sp[1] + "  ×  " + sp[0]);
                }

                setMeal.setTakeFood(takeFoodSetMeal);

                setMealArrayList.add(setMeal);
            }
        }

        // 塞入集合中
        orderDetailsVo.setVariety(varietyArrayList).setSetMeal(setMealArrayList);

        return orderDetailsVo;
    }
}
